package com.example.sys.speciality.controller;

import cn.hutool.core.util.IdUtil;
import com.example.core.controller.BaseTreeController;
import com.example.sys.speciality.service.SysSpecialityCategoryService;
import com.example.sys.speciality.vo.SysSpecialityCategoryVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@RequestMapping(value = "sysSpecialityCategory")
@RestController
public class SysSpecialityCategoryController extends BaseTreeController<SysSpecialityCategoryService, SysSpecialityCategoryVo> {

    @ResponseBody
    @GetMapping(value = "download")
    public void download(HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            byte[] bytes = this.getService().download();
            response.setCharacterEncoding("UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(IdUtil.fastUUID(), "UTF-8"));
            response.addHeader("Content-Length", "" + bytes.length);
            outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception e) {
            logger.error("文件下载报错: {}", e.getMessage());
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
